//
//  MCChooseMultiplePicturesCell.swift
//  CityPlatform
//
//  Created by MC on 2018/12/19.
//  Copyright © 2018 MC. All rights reserved.
//

import UIKit

import SnapKit

public protocol MCPickerImageCellDelegate: NSObjectProtocol {
    func pickerImageCell(_ view: MCPickerImageCell, deleteImageAtRow row: Int)
}


public class MCPickerImageCell: UICollectionViewCell {
    
    public var row = 0
    
    public weak var delegate : MCPickerImageCellDelegate?
    
    public var cancelImage: UIImage? {
        didSet {
            if cancelImage != nil {
                cancelButton.setImage(cancelImage, for: .normal)
            } else {
                let image = Bundle.mc_loadImage("image_delete", from: "MCPhotoLibraryBundle", in: "MCComponentFunction")

                cancelButton.setImage(image, for: UIControl.State.normal)
            }
        }
    }

    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.addSubview(showImageView)
        showImageView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self)
        }
        
        self.addSubview(cancelButton)
        cancelButton.snp.remakeConstraints { (make) ->Void in
            make.right.top.equalTo(self)
            make.size.equalTo(CGSize.init(width: 24, height: 24))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func cancelButtonClicked(sender:UIButton) {
        delegate?.pickerImageCell(self, deleteImageAtRow: row)
    }
        
    lazy var showImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.borderColor = UIColor.mc_line.cgColor
        imageView.layer.borderWidth = 1
        imageView.contentMode = UIView.ContentMode.scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    lazy var cancelButton: UIButton = {
        let button = UIButton.init(type: UIButton.ButtonType.custom)

        button.addTarget(self, action: #selector(cancelButtonClicked), for: UIControl.Event.touchUpInside)
        return button
    }()
}
